package com.roc.web.action.bankcard;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.service.IPagenitionService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigMerinfoUtil;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
/**
 * @author zbl
 * @detail  选择银行信息和MCC
 * @version 0.1 选择银行信息  2016-12-05
 * @version 0.2 新增选择MCC信息 2017-02-11
 * */

@SuppressWarnings("serial")
@Component
@Scope("prototype")
@Namespace("/chooseBank")
public class BankCardQueryAction extends PaginationBaseAction{

    private Logger logger=LogManager.getLogger(BankCardQueryAction.class);
	
	private BankCardQuerySearchCondition searchCondition;
	
	@Autowired
	private IPagenitionService pagenitionService;
	
	
	@Autowired
	private IPageService  pageService;
	
	
	@Action(value = "init", results = { @Result(name = SUCCESS, location = "/pages/bankCard/bankCardQuery.jsp")
    })
	public String init(){
		try {
			
			getRequest().setAttribute("bankList", ConfigProperties.getSysDB1("UP_BC_CD",true));//银行信息
		} catch (Exception e) {
			logger.error("---初始化页面出错----",e);
		}
		
		return SUCCESS;
	}
	@Action(value = "queryBankList", 
			results = { @Result(name = SUCCESS, location = "/pages/bankCard/bankCardQuery.jsp"),
			@Result(name = ERROR, location = "/error.jsp")
    })
	public String queryBankCard(){
		Operators op = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(searchCondition==null){
			searchCondition=(BankCardQuerySearchCondition)getSearchConditionFromSession();
			if(searchCondition==null){
				searchCondition=new BankCardQuerySearchCondition();
			}
		}
		try {
			getRequest().setAttribute("bankList", ConfigProperties.getSysDB1("UP_BC_CD",true));//银行信息
			
			int rows=pageService.count(searchCondition);
			if(rows>0){
				searchRsList=pageService.findAll(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			}
			
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			if(null!=searchCondition.getBankName() && !searchCondition.getBankName().equals("")){
				getRequest().setAttribute("oneName", searchCondition.getBankName());
			}
		}catch(ServiceException e){
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	
	
	@Action(value = "initMcc", results = { 
			@Result(name = SUCCESS, location = "/pages/bankCard/chooseMcc.jsp")
    })
	public String initMcc(){
		try {
			getRequest().setCharacterEncoding("utf-8");
			String mccName=getRequest().getParameter("mccName");
			if(null==mccName || mccName.equals("")){
				getRequest().setAttribute("mccList", ConfigProperties.getHyMccTypeProperties());//mcc
			}else{
				String a=new String(mccName.getBytes("ISO-8859-1"),"utf-8");
				getRequest().setAttribute("mccList", ConfigMerinfoUtil.getMccByName(a));//mcc
			}
		} catch (Exception e) {
			logger.error("---加载MCC类别出错----",e);
		}
		return SUCCESS;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		// TODO Auto-generated method stub
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		// TODO Auto-generated method stub
		getSession().setAttribute(SEARCH_SESSION_BANK_CARD_QUERY, searchCondition);
		
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_BANK_CARD_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}
	public BankCardQuerySearchCondition getSearchCondition() {
		return searchCondition;
	}
	public void setSearchCondition(BankCardQuerySearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

}
